//
//  AutoSearchView.m
//  Dispatch
//
//  Created by hyeyeon on 14. 1. 16..
//  Copyright (c) 2014년 hyeyeon. All rights reserved.
//

#import "AutoSearchView.h"

@implementation AutoSearchView
@synthesize mTableView, mList, mWord, delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        mList = [[NSMutableArray alloc] init];
        [self makeObject];
    }
    return self;
}
/*

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    self.frame = rect;
    mTableView.frame = CGRectMake(10, 0, rect.size.width, rect.size.height-20);
    
    [mTableView reloadData];
    [super drawRect:rect];
}*/

-(void)setTableWithThisRect:(CGRect)rect{
    self.frame = rect;
    mTableView.frame = CGRectMake(10, 10, rect.size.width, rect.size.height-10);
    [mTableView reloadData];
}

#pragma mark - 생성
-(void)makeObject{
    mTableView = [[UITableView alloc] initWithFrame:CGRectZero];
    mTableView.delegate = self;
    mTableView.dataSource = self;
    mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    mTableView.backgroundColor = [UIColor clearColor];
    mTableView.showsHorizontalScrollIndicator = NO;
    mTableView.showsVerticalScrollIndicator = NO;
    [self addSubview:mTableView];
}

#pragma mark - UITableView Delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.mList count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 25;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *cellID = @"autoSearch";
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        
    }
    
    NKLabel *tLabel = (NKLabel*)[cell.contentView viewWithTag:100];
    if (tLabel && [tLabel superview]) {
        [tLabel removeFromSuperview];
    }
    
    NKLabel *title = [[NKLabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 20)];
    title.tag = 100;
    [title setText:[self.mList objectAtIndex:indexPath.row]];
    [title setFont:[UIFont boldSystemFontOfSize:14.0f]];
    [title setColorWord:mWord withColor:ColorFromRGB(0x49a1d9)];
    [cell.contentView addSubview:title];
    
//    NSLog(@"auto : %@",title.text);
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSLog(@"index : %d",indexPath.row);
    
    SearchView *searchView = (SearchView*)self.delegate;
    NSString *keyword = [self.mList objectAtIndex:indexPath.row];
    [searchView autoKeyword:keyword];
}


@end
